package com.zyl.Ecs.auth.web.controller;

import com.zyl.Ecs.common.domain.auth.ext.AccountExt;
import com.zyl.Ecs.common.domain.auth.vo.AccountVO;
import com.zyl.Ecs.auth.service.AccountService;
import com.zyl.Ecs.common.response.Result;
import com.zyl.Ecs.common.response.ResultCode;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @version v1.0
 * @ProjectName: Ecs
 * @Package: com.zyl.Ecs.auth.web.controller
 * @ClassName: AccountController
 * @Description TODO (用户控制器)
 * @Author 翟玉龙
 * @Date 2024/12/4-18:59
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/account")
public class AccountController {

    private final AccountService accountService;

    /**
     * 新增用户
     *
     * @param accountVO 用户VO
     * @return {@link Result }<{@link ? }>
     */
    @PostMapping
    public Result<?> add(@RequestBody AccountVO accountVO) {
        accountService.add(AccountExt.toExt(accountVO));
        return Result.success();
    }

    /**
     * 删除用户
     *
     * @param id 用户id
     * @return {@link Result }<{@link ? }>
     */
    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable("id") Integer id) {
        accountService.delete(id);
        return Result.success();
    }

    /**
     * 修改用户
     *
     * @param accountVO 用户VO
     * @return {@link Result }<{@link ? }>
     */
    @PutMapping
    public Result<?> update(
            @RequestBody AccountVO accountVO
    ) {
        accountService.update(AccountExt.toExt(accountVO));
        return Result.success();
    }

    /**
     * 查询用户列表
     *
     * @return {@link Result }<{@link ? }>
     */
    @GetMapping
    public Result<?> list() {
        return Result.success(
                Optional.ofNullable(accountService.getList())
                        .map(list -> list.stream()
                        .map(AccountVO::toVO)
                        .collect(Collectors.toList()))
                .orElse(null))
                .apply(ResultCode.SUCCESS.getMessage());
    }
}
